package Proxy;

import util.LogUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Logger;

public class LogProxy implements InvocationHandler {
    private Object object;

    //    public LogProxy(Object object) {
//        this.object = object;
//    }
    Logger logger = LogUtils.getLogger();

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ZonedDateTime zdf = ZonedDateTime.now();
        String sDate = zdf.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        logger.info(sDate);
        logger.info("--执行了" + method.getName() + "方法");
        Object result = method.invoke(object, args);
        logger.info("--结果是：" + result + "\n");
        return result;
    }

    public Object createPorxy(Object object) {
        this.object = object;
//        Proxy.newProxyInstance()
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
    }

}
